package peace.gui.controls.box
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.DropShadowFilter;
import flash.text.TextField;

import peace.gui.controls.ExistenceEvent;
import peace.gui.controls.RubberSprite;
import peace.gui.controls.buttons.PushButton;
/**
* 
*/
[Event(name="close", type="peace.gui.controls.ExistenceEvent")]
/**
 * 
 * Оболочка с тайтл-баром и кнопкой закрытия
 * 
 * @author peace
 * 
 */
public class Panel extends RubberSprite
{
	/**
	 * 
	 */
	public var content:DisplayObject;
	/**
	 * 
	 */
	public var title:Title = new Title();
	/**
	 * 
	 */
	protected var skin:Skin;
	/**
	 * 
	 * @param content
	 * @param background
	 * @param titleLabel
	 * @param closeButton
	 * @param titleBackground
	 * 
	 */
	public function Panel (content:DisplayObject,
							background:DisplayObject = null,
							titleLabel:TextField = null,
							closeButton:PushButton= null,
							titleBackground:DisplayObject = null
							)
	{
		super();
		
		addChild(title);
		
		this.content = content;
		addChild(content);
		
		dropShade();
		
		// если передали хоть какой скин, занчит чото замышляют таки
		if (background || titleLabel || closeButton || titleBackground)
		{
			setSkin(background, titleLabel, closeButton, titleBackground);
		}
	}
	/**
	 * 
	 * Заголовок панели
	 * 
	 * @param text
	 * 
	 */	
	public function set label (text:String):void
	{
		if (skin && title.label)
		{
			title.label.text = text;
		}
	}
	/**
	 * 
	 * Заголовок панели
	 * 
	 * @return 
	 * 
	 */
	public function get label ():String
	{
		if (skin && title.label)
		{
			return title.label.text;
		}
		return null;
	}
	/**
	 * 
	 * Видимость кнопки закрытия
	 * 
	 * @param b
	 * 
	 */	
	public function set hasCloseButton (b:Boolean):void
	{
		if (skin && skin.closeButton)
		{
			skin.closeButton.visible = b;
		}
	}
	/**
	 * 
	 * @param background
	 * @param titleLabel
	 * @param closeButton
	 * @param titleBackground
	 * 
	 */	
	public function setSkin (background:DisplayObject = null,
								titleLabel:TextField = null,
								closeButton:PushButton= null,
								titleBackground:DisplayObject = null):void
	{
		if (skin && skin.background && background)
		{
			removeChild(skin.background);
		}
		
		if (!skin) skin = new Skin(background, titleLabel, closeButton, titleBackground)
		else
		{
			if (background) skin.background = background;
			if (titleLabel) skin.title = titleLabel;
			if (closeButton) skin.closeButton = closeButton;
			if (titleBackground) skin.titleBackground = titleBackground;
		}
		
		if (skin.background)
		{
			addChild(skin.background);
		}
		
		title.setSkin(titleBackground, titleLabel, closeButton);
		
		if (title.close)
		{
			title.close.addEventListener(MouseEvent.CLICK, onClose, false,0,true);
		}
		
		widthChanged = heightChanged = true;
		arrange();
	}
	/**
	 * 
	 * 
	 */	
	override public function arrange():void
	{
		if (!skin) return;
		
		if (widthChanged)
		{
			title.width = size.width;
			if (skin.background)
			{
				skin.background.width = size.width;
				skin.background.y = title.height;
			}
		}
		if (heightChanged)
		{
			if (skin.background)
			{
				skin.background.height = size.height;
			}
		}
		
		content.y = title.height;
	}
	/**
	 * 
	 * @param e
	 * 
	 */	
	private function onClose (e:Event):void
	{
		close();
	}
	/**
	 * virtual <br>
	 * 
	 */	
	public function close ():void
	{
		dispatchEvent(new ExistenceEvent(ExistenceEvent.CLOSE));
	}
	/**
	 * 
	 * 
	 */	
	protected function dropShade ():void
	{
		var shade:DropShadowFilter = new DropShadowFilter(3,90,0,0.2,4,4);
		shade.quality = BitmapFilterQuality.HIGH;
		filters = [shade];													
	}
	
	
}
}
import flash.text.TextField;
import peace.gui.controls.buttons.PushButton;
import flash.display.DisplayObject;
import flash.display.Shape;
import peace.gui.controls.RubberSprite;

/**
 * 
 * @author peace
 * 
 */
internal class Title extends RubberSprite
{
	
	/**
	 * 
	 */
	public var background:DisplayObject;
	/**
	 * 
	 */
	public var label:TextField;
	/**
	 * 
	 */
	public var close:PushButton;
	/**
	 * 
	 * @param skin
	 * @param label
	 * @param close
	 * 
	 */	
	public function Title ()
	{
	}
	/**
	 * 
	 * 
	 */	
	override public function arrange():void
	{
		if (widthChanged)
		{
			if (background)
			{
				background.width = size.width;
			}
			var closeWidth:Number = 0;
			if (close)
			{
				close.x = size.width - close.width - 5;
				close.y = 5;
				closeWidth = close.width + 5 + 5;
			}			
			if (label)
			{
				label.x = 5;
				label.y = 5;
				label.width = size.width - 5 - closeWidth;
			}
		}
		
		var maxHeight:Number = 0;
		if (close) maxHeight = Math.max(close.height, maxHeight);
		if (label) maxHeight = Math.max(label.height, maxHeight);
		if (background) 
		{
			maxHeight = Math.max(background.height, maxHeight);
			background.height = maxHeight;
		}
	}
	/**
	 * 
	 * @param background
	 * @param label
	 * @param close
	 * 
	 */	
	public function setSkin (background:DisplayObject = null, label:TextField = null, close:PushButton = null):void
	{
		if (background)
		{
			if (this.background) removeChild(this.background);
			this.background = background;
			addChild(background);	
		}
		if (label)
		{
			if (this.label) removeChild(this.label);
			this.label = label;
			addChild(label);	
		}
		if (close)
		{
			if (this.close) removeChild(this.close);
			this.close = close;
			addChild(close);
		}
		
		widthChanged = true;
		arrange();
	}
	
	
	
}

/**
 * 
 * @author peace
 * 
 */
internal class Skin
{
	public var background:DisplayObject;
	public var title:TextField;
	public var closeButton:PushButton;
	public var titleBackground:DisplayObject;	
	
	public function Skin (background:DisplayObject = null,
							title:TextField = null,
							closeButton:PushButton= null,
							titleBackground:DisplayObject = null)
	{
		this.background = background || getDefaultBg();
		this.title = title || getDefaultLabel();
		this.closeButton = closeButton || getDefaultClose();
		this.titleBackground = titleBackground || getDefaultTitleBg();
	}
	/**
	 * 
	 * @return 
	 * 
	 */
	private function getDefaultBg ():Shape
	{
		var s:Shape = new Shape();
		s.graphics.beginFill(0xffffff);
		s.graphics.drawRect(0,0,10,10);
		return s;
	}
	/**
	 * 
	 * @return 
	 * 
	 */
	private function getDefaultTitleBg ():Shape
	{
		return null;
	}
	/**
	 * 
	 * @return 
	 * 
	 */
	private function getDefaultLabel ():TextField
	{
		return null;
	}
	/**
	 * 
	 * @return 
	 * 
	 */
	private function getDefaultClose ():PushButton
	{
		return null;
	}
		
	
}